using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration.Design.MatchingRules;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Validation;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration;
using System.ComponentModel;

namespace EntLibContrib.PolicyInjection.Design
{
    class NotMatchingRuleNode : MatchingRuleNode, ICompositeMatchingRuleNode
    {
        NotMatchingRuleData ruleData;

        public NotMatchingRuleNode()
            :this(new NotMatchingRuleData(Resources.NotMatchingRuleName))
        {
        }

        public NotMatchingRuleNode(NotMatchingRuleData ruleData)
            :base(ruleData)
        {
            this.ruleData = ruleData;
        }

        public override void Validate(IList<ValidationError> errors)
        {
            if (Nodes.Count != 1)
            {
                errors.Add(new ValidationError(this, String.Empty, Resources.NotMatchingRuleShouldContainExactlyOneChildNode));
            }
            base.Validate(errors);
        }

        public override Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration.MatchingRuleData GetConfigurationData()
        {
            NotMatchingRuleData ruleData = new NotMatchingRuleData(this.Name);
            foreach (MatchingRuleNode node in this.Nodes)
            {
                ruleData.MatchingRules.Add(node.GetConfigurationData());
                break;
            }
            return ruleData;
        }

        #region ICompositeMatchingRuleNode Members

        [Browsable(false)]
        public IEnumerable<MatchingRuleData> ChildRules
        {
            get
            {
                return ruleData.MatchingRules;
            }
        }

        #endregion
    }
}
